//
//  StatusCell.swift
//  weibobbaj
//
//  Created by Tiffany宝 on 16/4/20.
//  Copyright © 2016年 Tiffany. All rights reserved.
//

import UIKit
import SnapKit

// 头像的大小
let StatusCellHeadImgWH: CGFloat = 40

// 昵称的字体大小
let StatusCellNameFontSize: CGFloat = 14
// 时间与来源
let StatusCellTimeFontSize: CGFloat = 10
// cell里面公共的间距
let StatusCellMargin: CGFloat = 10

//微博内容(正文/转发)Label
let StatusCellContentFontSize: CGFloat = 14

// 底部视图的高度
let StatusCellBottomViewH: CGFloat = 30

class StatusCell: UITableViewCell {
    
    var toolBarTopConstraint: Constraint?
    
    var statusViewModel: CellStatusViewModel?{
        
        didSet{
            
            originView.statusViewModel = statusViewModel
            
            //逻辑处理在 cell 中进行
            //拿到模型 就进行 转发微博内容的 判断逻辑
            
            //解决约束冲突的办法 1.记录下冲突的约束 2.使该冲突约束失效(uninstall)
            toolBarTopConstraint?.uninstall()
            
            if statusViewModel?.status?.retweeted_status == nil{
                
                //没有 转发微博View
                retweet.hidden = true
                
                toolbar.snp_updateConstraints(closure: { (make) -> Void in
                    
                    //记录最新的约束,防止拖动 cell 时,cell 重用所引起的约束冲突
                    toolBarTopConstraint = make.top.equalTo(originView.snp_bottom).constraint
                })
                
            }else{
                
                retweet.statusViewModel = statusViewModel
                
                //显示 转发微博View
                retweet.hidden = false
                
                toolbar.snp_updateConstraints(closure: { (make) -> Void in
                    
                    toolBarTopConstraint = make.top.equalTo(retweet.snp_bottom).constraint
                })
            }
        }
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        //禁止cell的点击
        selectionStyle = .None
        
        backgroundColor = UIColor(white: 237/255, alpha: 1)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        
        //添加控件
        contentView.addSubview(originView)
        contentView.addSubview(toolbar)
        contentView.addSubview(retweet)
        
        //添加约束
        originView.snp_makeConstraints { (make) -> Void in
            
            //给 tableView 添加分割线
            make.top.equalTo(contentView).offset(StatusCellMargin)
            make.leading.trailing.equalTo(contentView)
            //先假定行高
            //            make.height.equalTo(100)
        }
        
        toolbar.snp_makeConstraints { (make) -> Void in
            
            toolBarTopConstraint = make.top.equalTo(retweet.snp_bottom).constraint
            make.leading.trailing.equalTo(contentView)
            make.height.equalTo(StatusCellBottomViewH)
            
            //******** ---------------
            // toolBar的底部等于 contenView的底部 --> contentView 的底部等于 toolBar 底部
            //            make.bottom.equalTo(contentView)
        }
        
        contentView.snp_makeConstraints { (make) -> Void in
            
            make.bottom.equalTo(toolbar)
            make.leading.top.trailing.equalTo(self)
        }
        
        retweet.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(originView.snp_bottom)
            make.leading.trailing.equalTo(originView)
            //假定一个高度
//            make.height.equalTo(44)
            
        }
        
    }
    
    // MARK: - 懒加载
    private lazy var originView: OriginalView = OriginalView()
    private lazy var toolbar: StatusToolBar = StatusToolBar()
    private lazy var retweet: RetweetView = RetweetView()
}
